#!/usr/bin/env python
# coding:utf-8

import pygtk;
pygtk.require('2.0');
import gtk;
import vte;

def xpm_label_box(parent, xpm_filename, label_text):
    
    box = gtk.HBox(False, 0);
    
    box.set_border_width(2);
    
    image = gtk.Image();
    
    image.set_from_file(xpm_filename);
    
    label = gtk.Label(label_text);
    
    box.pack_start(image, False, False, 3);
    
    box.pack_start(label, False, False, 3);
    
    image.show();
    label.show();
    
    return box;
    
    pass

class Buttons:
    
    def __init__(self):
        
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL);
        self.window.set_title("Image'd Buttons!");
        
        self.window.connect("destroy", lambda wid:gtk.main_quit());
        self.window.connect("delete_event", lambda wid, data=None:gtk.main_quit());
        
        self.button = gtk.Button();
        
        self.button.connect("clicked", self.btn_click, "cool button");
        
        self.box = xpm_label_box(self.window, "info.xmp", "cool button");
        
        self.button.add(self.box);
        
        self.box.show();
        self.button.show();
        
        self.window.add(self.button);
        
        self.window.show();
        
        pass
    
    def btn_click(self, widget, data=None):
        
        print "hello again-%s was press" % data;
        
        pass
    
    pass

class ToggleButton:
    
    def toggleClick(self, widget, data=None):
        
        print "%s was toggled %s" % (data, ("OFF", "ON")[widget.get_active()]);
        
        pass
    
    def __init__(self):
        
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL);
        
        self.window.set_title("toggle button");
        
        self.window.connect("delete_event", lambda wid, data=None:gtk.main_quit());
        
        self.window.set_border_width(20);
        
        vbox = gtk.VBox(True, 2);
        
        self.window.add(vbox);
        
        button = gtk.ToggleButton("toggle button1");
        
        button.connect("toggled", self.toggleClick, "toggle button1");
        
        vbox.pack_start(button, True, True, 2);
        
        button = gtk.ToggleButton("toggle button2");
        
        button.connect("toggled", self.toggleClick, "toggle button2");
        
        vbox.pack_start(button, True, True, 2);
        
        button = gtk.Button("Quit");
        
        button.connect("clicked", lambda wid, data=None:gtk.main_quit());
        
        vbox.pack_start(button, True, True, 2);
        
        self.window.show_all();
        
        pass
    
    
    pass

def scale_set_default_values(scale):
    
    scale.set_update_policy(gtk.UPDATE_CONTINUOUS);
    scale.set_digits(1);
    scale.set_value_pos(gtk.POS_TOP);
    scale.set_draw_value(False);
    
    pass

"""
@summary: 滚动条
"""
class RangeWidgets:
    
    def cb_draw_value(self, button):
        
        self.hscale.set_draw_value(button.get_active());
        
        self.vscale.set_draw_value(button.get_active());
        
        pass
    
    def valuechange(self, get, set):
        
        print get.value;
        
        pass
    
    def __init__(self):
        
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL);
        
        self.window.set_title("滚动条");
        
        self.window.connect("delete_event", lambda wid, data=None:gtk.main_quit());
        
        box1 = gtk.VBox(False, 0);
        
        self.window.add(box1);
        
        box2 = gtk.HBox(False, 10);
        
        box2.set_border_width(10);

        box1.pack_start(box2, True, True, 0);
        
        adj1 = gtk.Adjustment(0, 0, 101, 1, 1, 1);
        
        adj1.connect("value_changed", self.valuechange, adj1);
        
        self.vscale = gtk.VScale(adj1);
        
        scale_set_default_values(self.vscale);
        
        box2.pack_start(self.vscale, True, True, 0);
        
        box3 = gtk.VBox(False, 10);
        
        box2.pack_start(box3, True, True, 0);
        
        self.hscale = gtk.HScale(adj1);
        
        self.hscale.set_size_request(200, 30);
        
        scale_set_default_values(self.hscale);
        
        box3.pack_start(self.hscale, True, True, 0);
        
        scrollbar = gtk.HScrollbar(adj1);
        
        scrollbar.set_update_policy(gtk.UPDATE_CONTINUOUS);
        
        box3.pack_start(scrollbar, True, True, 0);

        self.processbar = gtk.ProgressBar();
        
        box3.pack_start(self.processbar, True, True, 0);
        
        self.processbar.set_fraction(10.0);
        
        adj2 = gtk.Adjustment(1, 1, 31, 1, 5, 0);
        
        spinner = gtk.SpinButton(adj2, 0, 0);
                
        box3.pack_start(spinner, True, True, 0);
        
        combo = gtk.Combo();
        
        slist = [ "String 1", "String 2", "String 3", "String 4" ];
        
        combo.set_popdown_strings(slist);
        
        box3.pack_start(combo, True, True, 0);
        
        self.window.show_all();
        
        pass
    
    pass

class FixedExample:
    
    def move_button(self, widget):
        
        self.x = (self.x + 30) % 300;
        self.y = (self.y + 50) % 300;
        self.fixed.move(widget, self.x, self.y);
        
        pass
    
    def __init__(self):
        
        self.x = 50;
        self.y = 50;
        
        window = gtk.Window(gtk.WINDOW_TOPLEVEL);
        
        window.set_title("Fixed");
        
        window.connect("destroy", lambda wid:gtk.main_quit());
        
        window.set_border_width(5);
        
        self.fixed = gtk.Fixed();
        
        window.add(self.fixed);
        
        for i in range(1, 4):
            
            button = gtk.Button("Press me");
            button.connect("clicked", self.move_button);
            self.fixed.put(button, i * 50, i * 50);
            
            pass
        
        window.show_all();
                            
        pass
    
    pass

class MenuExample:
    
    def menuitem_response(self, widget, string):

        print "%s" % string
        
        pass

    
    def __init__(self):
        
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        window.set_size_request(400, 300)
        window.set_title("GTK Menu Test")
        window.connect("delete_event", lambda w, e: gtk.main_quit())

        menu = gtk.Menu()
        
        for i in range(3):
            buf = "Test-undermenu - %d" % i

            menu_items = gtk.MenuItem(buf)

            menu.append(menu_items)
            
            menu_items.connect("activate", self.menuitem_response, buf)
            
            menu_items.show();
            
            pass
        
        root_menu = gtk.MenuItem("Root Menu")
        
        root_menu.set_submenu(menu);
        
        root_menu.show();
        
        vbox = gtk.VBox(False, 0)
        window.add(vbox)
        
        vbox.show();
        
        menu_bar = gtk.MenuBar()
        
        
        menu_bar.show();
        
        menu_bar.append (root_menu)
        
        vt = vte.Terminal();
        vt.connect("child-exited", lambda term: gtk.main_quit());
        vt.fork_command();
        # feed_child(cmd+"\n\r");
                
        vbox.pack_start(vt, True, True, 2)
        
        vbox.pack_start(menu_bar, False, False, 2)
        
        window.show_all();
        
        pass
    
    pass

class BasicTreeViewExample:
    
    def __init__(self):
        
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL);
        
        self.window.set_size_request(300, 400);
        
        self.window.connect("delete_event", lambda wid, data=None:gtk.main_quit());
        
        self.menu = gtk.Menu();
        
        self.menu.append(gtk.MenuItem("test1"));
        self.menu.append(gtk.MenuItem("test2"));
        self.menu.append(gtk.MenuItem("test3"));
        
        
        self.treestore = gtk.TreeStore(str);
        
        for parent in range(4):
            piter = self.treestore.append(None, ['parent %i' % parent])
            for child in range(3):
                self.treestore.append(piter, ['child %i of parent %i' % (child, parent)])
        
        self.treeview = gtk.TreeView(self.treestore);
        
        # self.treeview.connect("popup-menu", self.right_click_event);
        
        self.treeview.connect("button-release-event", self.right_click_event);
        
        self.window.add(self.treeview);
        
        self.tvcolumn = gtk.TreeViewColumn('Column 0');
        
        self.treeview.append_column(self.tvcolumn)
        
        self.cell = gtk.CellRendererText()
        
        self.tvcolumn.pack_start(self.cell, True)
        
        self.tvcolumn.add_attribute(self.cell, 'text', 0)
        
#        self.treeview.set_search_column(0)
#        
#        self.tvcolumn.set_sort_column_id(0)
#        
#        self.treeview.set_reorderable(True)
        
        self.window.show_all();
#        self.window.set_visible(False)
        
        pass
    
    def right_click_event(self, widget, event):
        print event;
        if(event.button == 3):
            x = int(event.x)
            y = int(event.y)
            pthinfo = self.treeview.get_path_at_pos(x, y)
            if pthinfo is not None:
                
                print pthinfo;
                
                self.menu.show_all();
            
                self.menu.popup(None, None, None, event.button, event.time);
                
                pass
            
            pass
        
        pass
    
    pass

def Main():
    
    gtk.main();
    
    pass

if(__name__ == "__main__"):
    
    # btn = Buttons();
    
    # ToggleButton();
    
    # RangeWidgets();
    
    # FixedExample();
    
    # MenuExample();
    
    BasicTreeViewExample();
    
    Main();
    
    pass 
